package com.atguigu.gmall.publisher.controller;

import com.atguigu.gmall.publisher.bean.SubjectCommodityStats;
import com.atguigu.gmall.publisher.bean.SubjectOrderAmountPieGraph;
import com.atguigu.gmall.publisher.mapper.CommodityStatsMapper;
import com.atguigu.gmall.publisher.service.SubjectStatsService;
import com.atguigu.gmall.publisher.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("com.atguigu.gmall.publisher.mapper.CommodityStatsMapper")
public class CommodityStatsController {

    @Autowired
    private SubjectStatsService  subjectStatsService;

    @RequestMapping("com.atguigu.gmall.publisher.mapper.CommodityStatsMapper")
public String  getSubjectCommodityStats (
            @RequestParam(value = "date", defaultValue = "1") Integer date){
        if (date == 1){
            date = DateUtil.now();
        }
        List<SubjectCommodityStats> SubjectCommodityStatsList = subjectStatsService.getSubjectCommodityStatService(date);
        if(SubjectCommodityStatsList==null){
            return "";
        }
        StringBuilder rows = new StringBuilder("[");
        for (int i = 0; i < SubjectCommodityStatsList.size(); i++)
        {
            SubjectCommodityStats subjectCommodityStats = SubjectCommodityStatsList.get(i);
            String subjectName = subjectCommodityStats.getSubject_name();
            Integer orderCount = subjectCommodityStats.getOrderCount();
            Integer courseId = subjectCommodityStats.getCourse_id();
            Double finalAmount = subjectCommodityStats.getFinal_amount();

            rows.append("{\n" +
                    "\t\"subject_name\": \"" + subjectName + "\",\n" +
                    "\t\"orderCount\": \"" + orderCount + "\",\n" +
                    "\t\"course_id\": \"" + courseId + "\",\n" +
                    "\t\"final_amount\": \"" + finalAmount + "\",\n" +
                    "}");
            if (i < SubjectCommodityStatsList.size() - 1) {
                rows.append(",");
            } else {
                rows.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"科目名称\",\n" +
                "        \"id\": \"subject_name\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"订单数\",\n" +
                "        \"id\": \"orderCount\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"课程id\",\n" +
                "        \"id\": \"course_id\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"金额\",\n" +
                "        \"id\": \"final_amount\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": " + rows + "\n" +
                "  }\n" +
                "}";

    }

    @RequestMapping("com.atguigu.gmall.publisher.mapper.CommodityStatsMapper")
    public String SubjectOrderAmountPieGraph(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if(date == 1) {
            date = DateUtil.now();
        }
        List<SubjectOrderAmountPieGraph> subjectOrderAmountPieGraphList = subjectStatsService.getSubjectOrderAmountPieGraph(date);
        if(subjectOrderAmountPieGraphList == null || subjectOrderAmountPieGraphList.size() == 0) {
            return "";
        }
        StringBuilder dataSet = new StringBuilder("[");
        for (int i = 0; i < subjectOrderAmountPieGraphList.size(); i++) {
            SubjectOrderAmountPieGraph subjectOrderAmountPieGraph = subjectOrderAmountPieGraphList.get(i);
            String subjectName = subjectOrderAmountPieGraph.getSubject_name();
            Double finalAmount = subjectOrderAmountPieGraph.getFinal_amount();
            dataSet.append("{\n" +
                    "      \"subject_name\": \""+ subjectName +"\",\n" +
                    "      \"final_amount\": "+ finalAmount +"\n" +
                    "    }");
            if(i < subjectOrderAmountPieGraphList.size() - 1) {
                dataSet.append(",");
            } else {
                dataSet.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": "+ dataSet +"\n" +
                "}";
    }
}
